﻿using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using DotNetty.Common.Utilities;
using System.Text;
namespace Athena.Transport.DotNetty.ChannelHandlerAdapers
{
    using Codec;
    public class TransportMessageChannelHandlerAdaper : ChannelHandlerAdapter
    {
        private readonly IRpcMessageDecoder _rpcMessageDecoder;

        public TransportMessageChannelHandlerAdaper(IRpcMessageDecoder rpcMessageDecoder)
        {
            _rpcMessageDecoder = rpcMessageDecoder;
        }

        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            var byteBuffer = message as IByteBuffer;
            if (byteBuffer != null)
            {
                var rpcMessage = _rpcMessageDecoder.MessageDecoding(byteBuffer.ToString(Encoding.UTF8));
                context.FireChannelRead(rpcMessage);
                ReferenceCountUtil.Release(byteBuffer);
            }
        }
    }
}
